#import "BWTool.h"

#import "BWLayerSet.h"
#import "BWGridGeometry.h"
#import "BWCrossStitchLayer.h"
#import "BWLayerView.h"

@implementation BWTool

- (id) initWithGeometry: (BWGridGeometry *) g
                 layers: (BWLayerSet *) l
              layerView: (BWLayerView *) lv
{
    if (self = [super init]) {
        geometry = [g retain];
        layers = [l retain];
        layerView = [lv retain];
    }

    return (self);

} // initWithGeometry


- (void) dealloc
{
    [geometry release];
    [layers release];
    [layerView release];

    [super dealloc];

} // dealloc



- (BOOL) handleMouseDown: (NSEvent *) event
                 atPoint: (NSPoint) mouse
{
    return (NO);
} // handleMouseDown


- (void) handleMouseDragged: (NSEvent *) event
                    atPoint: (NSPoint) mouse
{
} // mouseMouseDragged

- (void) handleMouseUp: (NSEvent *) event
               atPoint: (NSPoint) mouse
{
} // handleMouseUp


- (void) handleKeyDown: (NSEvent *) event
               atPoint: (NSPoint) mouse
{
} // handleKeyDown


- (void) handleKeyUp: (NSEvent *) event
             atPoint: (NSPoint) mouse
{
} // handleKeyUp


- (void) handleFlagsChanged: (NSEvent *) event
                    atPoint: (NSPoint) mouse
{
} // handleFlagsChanged


- (void) invalidateAll
{
    [layerView setNeedsDisplay: YES];

} // invalidateAll


- (void) invalidateRow: (int) row
                column: (int) column
{
    NSRect rect = [geometry rectForRow: row
                            column: column];

    [layerView setNeedsDisplayInRect: rect];

} // invalidateRow



- (void) invalidateRectAt: (int) row
                   column: (int) column
                      row: (int) row2
                   column: (int) column2
{
    int minRow = MIN (row, row2);
    int maxRow = MAX (row, row2);
    int minColumn = MIN (column, column2);
    int maxColumn = MAX (column, column2);

    // ??? is this a hack?  in some situations (like dragging a
    // ??? cross stitch rectangle), one extremity wouldn't draw if the
    // ??? mouse were over a grid line

    NSRect rect = [geometry rectForRow: minRow - 1
                            column: minColumn - 1];
    NSRect rect2 = [geometry rectForRow: maxRow + 1
                             column: maxColumn + 1];

    NSRect inval = NSUnionRect (rect, rect2);

    [layerView setNeedsDisplayInRect: inval];

} // invalidateRectAt


- (unsigned int) capabilities
{
    return (kCapability_None);
} // capabilities


- (id) pickLayerForKind: (BWLayerKind) kind
{
    BWLayer *pickedLayer = nil;

    int i;
    for (i = 0; i < [layers count]; i++) {
        BWLayer *candidate = [layers layerAtIndex: i];
        
        if ([candidate layerKind] == kind
            && [candidate active]) {
            pickedLayer = candidate;
            break;
        }
    }

    return (pickedLayer);

} // pickLayerForKind


- (NSArray *) allLayersForKind: (BWLayerKind) kind
{
    NSMutableArray *kindLayers = [NSMutableArray array];

    int i;
    for (i = 0; i < [layers count]; i++) {
        BWLayer *candidate = [layers layerAtIndex: i];
        
        if ([candidate layerKind] == kind
            && [candidate active]) {
            [kindLayers addObject: candidate];
        }
    }

    return (kindLayers);

} // allLayersForKind



- (NSString *) name
{
    return (@"");
} // name

@end // BWTool


